//
//  CollectionViewController.m
//  Tiger
//
//  Created by King on 11-9-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CollectionViewController.h"
#import "ProductListViewCell.h"
#import "ProductDetailViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ContactInfoVController.h"

@implementation CollectionViewController
@synthesize tableView = tableView_;
@synthesize dataList;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
//-(void)viewDidAppear:(BOOL)animated
//{
//    //读取plist
//	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//	NSString *documentsDirectory = [paths objectAtIndex:0];
//	NSString *path = [documentsDirectory stringByAppendingPathComponent:@"noteData.plist"];
//	dataList = [[NSMutableArray alloc] initWithContentsOfFile:path];
//	
//	NSLog(@"dataList = %@", dataList);
//	[super viewDidAppear:animated];
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"] forBarMetrics: UIBarMetricsDefault];
        self.navigationController.navigationBar.clipsToBounds = YES;
        
    }
	[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top.png"]];
	self.navigationController.navigationBar.tintColor =  [UIColor colorWithRed:33.0/255 green:32.0/255 blue:32.0/255 alpha:1.0];
	
	self.navigationItem.rightBarButtonItem = [self editButtonItem];		
	
	self.tableView.separatorStyle = NO;
	
    UIButton* tApplyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    tApplyButton.frame =CGRectMake(30, 352, 260, 34);
    [tApplyButton setBackgroundColor:[UIColor clearColor]];
    [tApplyButton setBackgroundImage:[UIImage imageNamed:@"more_top.png"] forState:UIControlStateNormal];
    [tApplyButton setTitle:@"提交清单" forState:UIControlStateNormal];
    [tApplyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tApplyButton addTarget:self action:@selector(applyMonad) forControlEvents:UIControlEventTouchUpInside];
    tApplyButton.layer.cornerRadius =4.f;
    tApplyButton.layer.masksToBounds =YES;
    [self.view addSubview:tApplyButton];
	/***********************************************************************/
//	//读取plist
//	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//	NSString *documentsDirectory = [paths objectAtIndex:0];
//	NSString *path = [documentsDirectory stringByAppendingPathComponent:@"noteData.plist"];
//	dataList = [[NSMutableArray alloc] initWithContentsOfFile:path];
//	
//	NSLog(@"dataList = %@", dataList);
	
}

-(void) applyMonad{
    ContactInfoVController* tNextVC = [[ContactInfoVController alloc] init];
    [self.navigationController pushViewController:tNextVC animated:YES];
    [tNextVC release];
}

#pragma mark super
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
	[super setEditing:editing animated:animated];
	[self.tableView setEditing:editing animated:animated];
}

-(void)reloadData {
    //读取plist
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *path = [documentsDirectory stringByAppendingPathComponent:@"noteData.plist"];
	self.dataList = [[[NSMutableArray alloc] initWithContentsOfFile:path] autorelease];
	
	//NSLog(@"dataList = %@", dataList);
    [self.tableView reloadData];
}

#pragma mark Table view methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 49.0;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	return [dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString* CellIdentifier = @"Cellidentifier";
	UIImageView* bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabe_view.png"]];
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
									   reuseIdentifier:CellIdentifier] autorelease];

    }
	int index = indexPath.row;
	NSDictionary *dict = [dataList objectAtIndex:index];
	cell.textLabel.text = [dict objectForKey:@"productName"];
	cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
	cell.indentationLevel = 2;
	cell.indentationWidth = 4;
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	cell.backgroundView = bgImageView;
	[bgImageView release];
	return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(editingStyle==UITableViewCellEditingStyleDelete) {
		
		int index = indexPath.row;		
		[dataList removeObjectAtIndex:index];
		
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0];
		NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"noteData.plist"];
		[dataList writeToFile:writableDBPath atomically:YES];
		[self.tableView reloadData];
	}
}	
	
#pragma mark - Table view delegate	
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	ProductDetailViewController *viewController = [[ProductDetailViewController alloc] initWithNibName:@"ProductDetailViewController" bundle:nil];
	viewController.hidesBottomBarWhenPushed = YES;
	NSDictionary *dict = [dataList objectAtIndex:indexPath.row];
	viewController.productID = [[dict objectForKey:@"productID"] intValue];
	//导航自带的动画
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
								   initWithTitle:@"订购清单" 
								   style:UIBarButtonItemStylePlain 
								   target:self
								   action:nil];
	self.navigationItem.backBarButtonItem = backButton;
	[self.navigationController pushViewController:viewController animated:YES];
	[viewController release];
}
	

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

	self.tableView = nil;

}


- (void)dealloc {
    [super dealloc];
	[tableView_ release];
	[dataList release];

}


@end
